from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

#Applicant Report Model Class
class modelReport(db.Model):
	Applicant = db.StringProperty()
	Reportblob = db.BlobProperty()

#Admin Page Setup
class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/html'
        self.response.out.write('''
<html><body>ResEdge Admin Page<p><p>
</form><hr>
<form id="frmUploadAppReport" action="/admin/upload_results" method="POST" enctype="multipart/form-data">Upload File: <input type="file" name="file"><br>
Applicant Username: <input type="text" name="txtApplicantName" /><br>
<input type="submit" name="submit" value="Submit">
</form>
<p></p>
<a href="/admin/upload_results">Upload Results</a></p>
<a href="/admin/retrieve_results">Retrive Results</a></p>
<a href="/">Home Page</a></p>
<a href="/admin/">Admin Page</a></p>
<hr>	
''')
#End of Admin Page Setup

#Store Applicant Report Class
class UploadResults(webapp.RequestHandler):
	def post(self):
		m = modelReport()
		m.Applicant = self.request.get("txtApplicantName")
		Reportblob = self.request.get("file")
		m.Reportblob = db.Blob(Reportblob)
		m.put()
		self.redirect('/')

#Display Applicant Report Class
class RetrieveResults(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/html'
        self.response.out.write('<html><body>Results Are As Follows:<p>')
        reports = db.GqlQuery('SELECT * FROM modelReport')
        for report in reports:
            self.response.out.write('</p><b>%s</b> report is:' % report.Applicant)
            self.response.out.write("<div><img src='img?img_id=%s'></img>" % report.key())
#            self.response.out.write(' %s</div>' % cgi.escape(report.content))  #'content' was a field in the Guestbook application
            
application = webapp.WSGIApplication([
           ('/admin/', MainPage),
           ('/admin/upload_results', UploadResults),
           ('/admin/retrieve_results', RetrieveResults),
          ], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()